package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.common.BedPutSearchParams;
import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.dao.*;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.AddressService;
import com.bed.lovewithpatientbed.service.BedService;
import com.bed.lovewithpatientbed.service.DeptService;
import com.bed.lovewithpatientbed.service.HospitalService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName BedServiceImpl.java
 * @author sunningsheng
 * @version 1.0.0
 * @Description 床位管理Service实现类
 * @createTime
 */
@Service
public class BedServiceImpl implements BedService {

    @Resource
    private BedPutDao bedPutDao;
    @Resource
    private BedDao bedDao;
    @Resource
    private HospitalDao hospitalDao;
    @Resource
    private DeptDao deptDao;
    @Resource
    private BedStatusDao bedStatusDao;
    @Resource
    private AddressService addressService;
    @Resource
    private HospitalService hospitalService;
    @Resource
    private DeptService deptService;

    @Override
    public long countRow() {

        BedPutExample bedPutExample = new BedPutExample();
        BedPutExample.Criteria criteria = bedPutExample.createCriteria();
        criteria.andBedStatusIdNotEqualTo(2);
        return bedPutDao.countByExample(bedPutExample);
    }

    /**
     * @title bedPut
     * @description 床位投放的实现方法
     * @author admin
     * @updateTime
     * @throws
     */
    @Override
    public void bedPut(BedPut bedPut) {
        bedPutDao.insert(bedPut);
    }

    /**
     * @title addBed
     * @description 新增床位的实现方法
     * @author sunningsheng
     * @updateTime   
     * @throws 
     */
    @Override
    public void bedAdd(Bed bed) {
        bedDao.insert(bed);
    }

    /**
     * @title bedUpdate
     * @description 床位信息修改(医院、科室)
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public void bedUpdate(BedPut bedPut) {
        bedPutDao.updateByPrimaryKeySelective(bedPut);
    }

    /**
     * @title bedSelectBybid
     * @description 根据ID查询床位
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public BedPut bedSelectBybid(Integer bedPutId) {
        BedPut bedPut = bedPutDao.selectByPrimaryKey(bedPutId);

        Bed bed = bedDao.selectByPrimaryKey(bedPut.getBedId());
        Hospital hospital = hospitalDao.selectByPrimaryKey(bedPut.getHospitalId());
        Dept dept = deptDao.selectByPrimaryKey(bedPut.getDeptId());
        BedStatus bedStatus = bedStatusDao.selectByPrimaryKey(bedPut.getBedStatusId());

        bedPut.setBed(bed);
        bedPut.setHospital(hospital);
        bedPut.setDept(dept);
        bedPut.setBedStatus(bedStatus);
        return bedPut;
    }

    /**
     * @title bedSelect
     * @description 固定条件筛选床位(分页展示)
     * @author admin
     * @updateTime
     * @throws
     */
    @Override
    public List<BedPut> bedSelect(PageBean pageBean,BedPut bedPut) {

        //封装PageBean
        BedPutExample bedPutExample = new BedPutExample();
        BedPutExample.Criteria criteria = bedPutExample.createCriteria();

        if (bedPut.getHospitalId()!=null&&!"".equals(bedPut.getHospitalId())){
            if (bedPut.getDeptId()!=null&&!"".equals(bedPut.getDeptId())){
                criteria.andHospitalIdEqualTo(bedPut.getHospitalId());
                criteria.andDeptIdEqualTo(bedPut.getDeptId());
            }
        }

        Long countRow = bedPutDao.countByExample(bedPutExample);
        pageBean.setCountRow(countRow.intValue());
        Integer countPage = pageBean.getCountPage()%countRow==0? pageBean.getCountPage()/pageBean.getPageSize() : pageBean.getCountPage()/ pageBean.getPageSize()+1;
        pageBean.setCountPage(countPage);

        //分页查询
        bedPutExample.setOffset((long) (pageBean.getCurrentPage()*pageBean.getPageSize()));
        bedPutExample.setLimit(countPage);

        criteria.andHospitalIdEqualTo(bedPut.getHospitalId());
        criteria.andDeptIdEqualTo(bedPut.getDeptId());
        criteria.andBedStatusIdEqualTo(bedPut.getBedStatusId());
        List<BedPut> bedPuts = bedPutDao.selectByExample(bedPutExample);
        return bedPuts;
    }

    /**
     * @title bedDeleteByPrimary
     * @description 删除床位信息(将状态ID设为2--删除)
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public void bedDeleteByPrimary(Integer[] bedPutIds) {
        BedPutExample bedPutExample = new BedPutExample();
        BedPutExample.Criteria criteria = bedPutExample.createCriteria();

        List<Integer> values = new ArrayList<>();
        //关机状态
        values.add(1);
        //待机状态
        values.add(5);
        //不能删除故障和使用中的床位
        criteria.andBedStatusIdIn(values);

        criteria.andBedPutIdIn(Arrays.asList(bedPutIds));

        BedPut bedPut = new BedPut();
        bedPut.setBedStatusId(2);
        bedPutDao.updateByExampleSelective(bedPut,bedPutExample);
    }

    /**
     * @title getCount
     * @description 投放的床位数
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public long getBedPutCount() {
        return bedPutDao.countByExample(null);
    }

    /**
     * @title getBedCount
     * @description 床位的总数量
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    public long getBedCount(){
        return bedDao.countByExample(null);
    }

    /**
     * @title bedSelectWithLimit
     * @description layui分页筛选已投放床位
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public List<BedPut> bedSelectWithLimit(LayUiPageBean pageBean, BedPutSearchParams bedPutSearchParams) {

        BedPutExample bedPutExample = new BedPutExample();
        BedPutExample.Criteria criteria = bedPutExample.createCriteria();
        HospitalExample hospitalExample = new HospitalExample();
        HospitalExample.Criteria criteria1 = hospitalExample.createCriteria();

        // 添加分页
        try {
            if (pageBean != null && pageBean.getPage() > 0 && pageBean.getLimit() > 0) {
                bedPutExample.setOffset((pageBean.getPage() - 1) * pageBean.getLimit());
                bedPutExample.setLimit(pageBean.getLimit().intValue());
            }

            //搜索条件
            if (bedPutSearchParams != null) {
                //床位投放id
                if (bedPutSearchParams.getBedPutId() != null && !"".equals(bedPutSearchParams.getBedPutId())) {
                    System.out.println("BedServiceImpl.bedSelectWithLimit" + bedPutSearchParams.getBedPutId());
                    criteria.andBedPutIdEqualTo(bedPutSearchParams.getBedPutId());
                }

                if (bedPutSearchParams.getAddressId() != null && !"".equals(bedPutSearchParams.getAddressId())){

                }
                //省份
                if (bedPutSearchParams.getProvince() != null && !"".equals(bedPutSearchParams.getProvince())) {
                    List<Address> addressList = addressService.selectByProvince(bedPutSearchParams.getProvince());
                    List<Integer> addressIds = new ArrayList<>();
                    for (Address add : addressList) {
                        addressIds.add(add.getAddressId());
                    }
                    criteria1.andAddressIdIn(addressIds);
                    try {
                        List<Hospital> hospitalList = hospitalDao.selectByExample(hospitalExample);
                        List<Integer> hospitalIds = new ArrayList<>();
                        for (Hospital hos : hospitalList) {
                            hospitalIds.add(hos.getHospitalId());
                        }
                        criteria.andHospitalIdIn(hospitalIds);
                    } catch (Exception e) {

                    }
                }
                //城市
                if (bedPutSearchParams.getCity() != null && !"".equals(bedPutSearchParams.getCity())) {
                    List<Address> addressList = addressService.selectByCity(bedPutSearchParams.getCity());
                    List<Integer> addressIds = new ArrayList<>();
                    for (Address address : addressList) {
                        addressIds.add(address.getAddressId());
                    }
                    criteria1.andAddressIdIn(addressIds);
                    List<Hospital> hospitalList = hospitalDao.selectByExample(hospitalExample);

                    List<Integer> hospitalIds = new ArrayList<>();
                    for (Hospital hos : hospitalList) {
                        hospitalIds.add(hos.getHospitalId());
                    }
                    criteria.andHospitalIdIn(hospitalIds);
                }
                //区
                if (bedPutSearchParams.getDistrict() != null && !"".equals(bedPutSearchParams.getDistrict())){
                    List<Address> addressList = addressService.selectByDistrict(bedPutSearchParams.getDistrict());
                    List<Integer> addressIds = new ArrayList<>();
                    for (Address address : addressList) {
                        addressIds.add(address.getAddressId());
                    }
                    criteria1.andAddressIdIn(addressIds);
                    List<Hospital> hospitalList = hospitalDao.selectByExample(hospitalExample);
                    List<Integer> hospitalIds = new ArrayList<>();
                    for (Hospital hos : hospitalList) {
                        hospitalIds.add(hos.getHospitalId());
                    }
                    criteria.andHospitalIdIn(hospitalIds);
                }
                //医院
                if (bedPutSearchParams.getHospitalId() != null && !"".equals(bedPutSearchParams.getHospitalId())) {
                    criteria.andHospitalIdEqualTo(bedPutSearchParams.getHospitalId());
                }
                //科室
                if (bedPutSearchParams.getDeptId() != null && !"".equals(bedPutSearchParams.getDeptId())) {
                    criteria.andDeptIdEqualTo(bedPutSearchParams.getDeptId());
                }
            }
            criteria.andBedStatusIdNotEqualTo(2);
            List<BedPut> bedPutList = bedPutDao.selectByExample(bedPutExample);
            return bedPutList;
        } catch (Exception e) {
            return null;
        }
    }

    //获取省份
    @Override
    public List<Address> selectProvince() {
        List<Address> addressList = addressService.selectByDistinct();
        return addressList;
    }

    //根据省份获取城市
    @Override
    public List<Address> selectCity(String province) {
        System.out.println("BedServiceImpl.selectCity");
        List<Address> addressList = addressService.selectCity(province);
        return addressList;
    }

    /**
     * @title selectDistrict
     * @description 根据城市获取区
     * @author sunningsheng
     * @updateTime
     * @throws
     */
    @Override
    public List<Address> selectDistrict(String city) {
        List<Address> addressList = addressService.selectDistrict(city);
        return addressList;
    }

    @Override
    public List<Hospital> selectHospital(String district) {
        List<Hospital> addressList = addressService.selectHospital(district);
        return addressList;
    }

    /**
         * @title 根据医院科室id查询科室使用床位情况
         * @description
         * @author yg
         * @updateTime
         * @throws
         */

    @Override
    public List<BedPut> selectByHospitalIdAndDeptId(Integer hospitalid,Integer deptid) {

        BedPutExample bedPutExample=new BedPutExample();
        BedPutExample.Criteria criteria = bedPutExample.createCriteria();

        List<BedPut> bedPutList=null;
        if (deptid!=null&&!"".equals(deptid)){
            if (hospitalid!=null&&!"".equals(hospitalid)){
                criteria.andDeptIdEqualTo(deptid);
                criteria.andHospitalIdEqualTo(hospitalid);
                bedPutList = bedPutDao.selectByExample(bedPutExample);
            }
        }

        return bedPutList;
    }
    @Override
    public void closeBed(Integer bedPutId) {
        BedPutExample bedPutExample = new BedPutExample();
        BedPutExample.Criteria criteria = bedPutExample.createCriteria();

        criteria.andBedPutIdEqualTo(bedPutId);
        criteria.andBedStatusIdEqualTo(5);

        BedPut bedPut = new BedPut();
        bedPut.setBedStatusId(1);
        bedPutDao.updateByExampleSelective(bedPut,bedPutExample);
    }
}
